﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace _1stVATCWin
{
    public partial class LienHe : Form
    {
        public string username;
        public LienHe()
        {
            InitializeComponent();
        }

        private void load_DSLH()
        {
            Config file = new Config(Application.StartupPath + "\\config.ini");
            string connectionString = file.ReadValue("server", "conn");
            //string connectionString = @"Data Source=FOX-PC\SQLEXPRESS;Initial Catalog=Project;Integrated Security=True";
            SqlConnection conn = new SqlConnection(connectionString);
            string sql = "select * from LIENHE";
            SqlCommand cmd = new SqlCommand(sql, conn);

            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            conn.Open();
            DataSet ds = new DataSet();
            adapter.Fill(ds, "Table LienHe");
            DataTable dt = ds.Tables["Table LienHe"];

            DSLH.DataSource = ds.Tables["Table LienHe"].DefaultView;
        }

        private void LienHe_Load(object sender, EventArgs e)
        {
            load_DSLH();
        }

        private void Update_Click(object sender, EventArgs e)
        {
            int i = DSLH.SelectedRows[0].Index;
            int id = int.Parse(DSLH.Rows[i].Cells["IDlienhe"].Value.ToString());

            Config file = new Config(Application.StartupPath + "\\config.ini");
            string connectionString = file.ReadValue("server", "conn");
            SqlConnection conn = new SqlConnection(connectionString);
            string sql = "update LIENHE set tenkhachhang = @name, email = @email,tieude = @td,noidung = @nd where IDlienhe = @id";
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.Parameters.AddWithValue("@id", id);
            cmd.Parameters.AddWithValue("@name", namelh.Text);
            cmd.Parameters.AddWithValue("@email", emaillh.Text);
            cmd.Parameters.AddWithValue("@td", tieudelh.Text);
            cmd.Parameters.AddWithValue("@nd", noidunglh.Text);

            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();

            load_DSLH();
        }

        private void Del_Click(object sender, EventArgs e)
        {
            if ((MessageBox.Show("Bạn có muốn xóa dữ liệu không?", "Thông Báo", MessageBoxButtons.OKCancel)) == DialogResult.OK)
            {
                int i = DSLH.SelectedRows[0].Index;
                int id = int.Parse(DSLH.Rows[i].Cells["IDlienhe"].Value.ToString());

                Config file = new Config(Application.StartupPath + "\\config.ini");
                string connectionString = file.ReadValue("server", "conn");
                //string connectionString = @"Data Source=FOX-PC\SQLEXPRESS;Initial Catalog=Project;Integrated Security=True";
                SqlConnection conn = new SqlConnection(connectionString);
                string sql = "delete from LIENHE where IDlienhe = @id";
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.Parameters.AddWithValue("@id", id);

                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();

                DSLH.Rows.RemoveAt(i);

                namelh.Text = "";
                emaillh.Text = "";
                tieudelh.Text = "";
                noidunglh.Text = "";                
            }
        }

        private void DSLH_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int id = int.Parse(DSLH.Rows[e.RowIndex].Cells["IDlienhe"].Value.ToString());

            Config file = new Config(Application.StartupPath + "\\config.ini");
            string connectionString = file.ReadValue("server", "conn");
            SqlConnection conn = new SqlConnection(connectionString);
            string sql = "select * from LIENHE where IDlienhe = @id";
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.Parameters.AddWithValue("@id", id);

            conn.Open();
            SqlDataReader r = cmd.ExecuteReader();
            while (r.Read())
            {
                namelh.Text = r["tenkhachhang"].ToString();
                emaillh.Text = r["email"].ToString();
                tieudelh.Text = r["tieude"].ToString();
                noidunglh.Text = r["noidung"].ToString();
            }
            r.Close();
            conn.Close();
        }
    }
}
